package com.qiyang.contoller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

@Controller
@RequestMapping("/i18n")
public class I18nController {

    /**
     * 自己实现
     * @param lang
     * @param country
     * @param localeResolver
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/i18n/{lang}_{country}")
    public String changeLocale(@PathVariable("lang")String lang, @PathVariable("country")String country,
                               @Autowired SessionLocaleResolver localeResolver,
                               HttpServletRequest request,
                               HttpServletResponse response){

        Locale local = new Locale(lang, country);

        localeResolver.setLocale(request, response, local);

        return "login";
    }

    /**
     * SpringMvc提供了一种更简单的方式来实现
     * 提供了内置拦截器 LocaleChangeInterceptor
     * 通过链接带 locale=zh_CN 参数切换
     */

}
